﻿using System.Linq;
using YawetagLib.System.Data;
using YawetagLib.System.Linq;
using YawetagLib.System.Utilities;

namespace YawetagDemo.Framework.Impl.Data
{
    internal class AlbumRankingRepository : DbModelBaseRepository<AlbumRanking>
    {
        public AlbumRankingRepository(IDataContext dataContext)
            : base(dataContext)
        {
        }

        public AlbumRanking GetOrDefault(int albumID, int personID)
        {
            return this.Where(i => i.AlbumID == albumID && i.PersonID == personID).SingleOrDefault();
        }

        public PagedResultInfo<AlbumRankingItem> GetPagedResult(string filterCriteria, string sortExpression, 
            int startRowIndex, int? maximumRows)
        {
            IQueryable<AlbumRankingItem> result = DataContext.GetAlbumRankingItems();

            if (!string.IsNullOrEmpty(filterCriteria))
            {
                result = result.Where(i => i.BandName.Contains(filterCriteria) || i.Name.Contains(filterCriteria));
            }

            return result.Sort(sortExpression).ToPagedResultSet(startRowIndex, maximumRows);
        }
    }
}
